package com.ruoyi.web.controller.system;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseArea;
import com.ruoyi.system.service.IBaseAreaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地区Controller
 * 
 * @author ruoyi
 * @date 2023-08-31
 */
@RestController
@RequestMapping("/system/baseArea")
public class BaseAreaController extends BaseController
{
    @Autowired
    private IBaseAreaService baseAreaService;

    /**
     * 查询地区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseArea baseArea)
    {
        startPage();
        List<BaseArea> list = baseAreaService.selectBaseAreaList(baseArea);
        return getDataTable(list);
    }

    /**
     * 导出地区列表
     */
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseArea baseArea)
    {
        List<BaseArea> list = baseAreaService.selectBaseAreaList(baseArea);
        ExcelUtil<BaseArea> util = new ExcelUtil<BaseArea>(BaseArea.class);
        util.exportExcel(response, list, "地区数据");
    }

    /**
     * 获取地区详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(baseAreaService.selectBaseAreaById(id));
    }

    /**
     * 新增地区
     */
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseArea baseArea)
    {
        return toAjax(baseAreaService.insertBaseArea(baseArea));
    }

    /**
     * 修改地区
     */
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseArea baseArea)
    {
        return toAjax(baseAreaService.updateBaseArea(baseArea));
    }

    /**
     * 删除地区
     */
    @Log(title = "地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(baseAreaService.deleteBaseAreaByIds(ids));
    }
    
    // 地区树
	@GetMapping("/alllist")
    public TableDataInfo getAreaTree()
    {
    	List<Area> result =  baseAreaService.getCityArea().stream().map(e->{
    		if(e.getParentId()==100000) {
    			e.setParentId(0);
    		}
    		return new Area(e);
    	}).collect(Collectors.toList());
        return getDataTable(result);
    }
    
    
}

/*临时方案 因为组件需要id与pid同为string类型故实行内部类模型转换*/
@SuppressWarnings("serial")
class  Area implements Serializable{

    /** 地区编号 */
    private Integer id;

    /** 父级地区编码 */
    @Excel(name = "父级地区编码")
    private Integer parentId;

    /** 等级 0-国家；1-省份；2-地级市；3-县区 */
    @Excel(name = "等级 0-国家；1-省份；2-地级市；3-县区")
    private Integer level;

    /** 名称 */
    @Excel(name = "名称")
    private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	

	public Area() {
		super();
	}
	public Area(BaseArea entity) {
		this.id = entity.getId();
		this.parentId = entity.getParentId();
		this.name = entity.getName();
		this.level = entity.getLevel();

	}
    
	
}


